package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.adapter.Processtask1Adapter;
import palmtop.com.palmtop.adapter.ProcesstaskAdapter;
import palmtop.com.palmtop.adapter.TaskgridAdapter;
import palmtop.com.palmtop.model.Processtask;
import palmtop.com.palmtop.model.Taskgrid;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.NetUtil;

public class ProcesstaskActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.processtaskList)
    ListView processtaskList;

    private ProcesstaskAdapter adapter;
    Taskgrid taskgrid;
    String userid;
    String name;
    String role_id;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_processtask, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(ProcesstaskActivity.this);
        ButterKnife.bind(this);
        taskgrid = (Taskgrid)getIntent().getSerializableExtra("taskgrid");
        AppPreferences appPreferences = new AppPreferences(ProcesstaskActivity.this);
        role_id = appPreferences.getString("role_id", "");
        initView();
    }

    private void initView() {
        mtitle.setText("任务完成情况");
        AppPreferences appPreferences = new AppPreferences(ProcesstaskActivity.this);
//        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        userid = appPreferences.getString("userid","");
        name = appPreferences.getString("name","");
        /*if(taskgrid!=null){
            RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/getListByTaskidAndUnitid");
            params.addBodyParameter("taskid",String.valueOf(taskgrid.getId()));
            params.addBodyParameter("regionid",String.valueOf(taskgrid.getUnitid()));
            NetUtil.postData(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    if(result!=null){
                        Gson gson = new Gson();
                        Type listType = new TypeToken<ArrayList<Processtask>>() {
                        }.getType();
                        final ArrayList<Processtask> list = gson.fromJson(result,listType);
                        adapter = new ProcesstaskAdapter(list,ProcesstaskActivity.this);
                        processtaskList.setAdapter(adapter);
                        processtaskList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent();
                                intent.putExtra("processtask",list.get(i));
                                intent.setClass(ProcesstaskActivity.this,ProcessdetailActivity.class);
                                startActivity(intent);
                            }
                        });
                    }
                }
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {}
                @Override
                public void onCancelled(CancelledException cex) {}
                @Override
                public void onFinished() {}
            });
        }*/

        if(taskgrid!=null){
            final ArrayList<Taskgrid> list = new ArrayList<Taskgrid>();
            if(!(taskgrid.getReceiverid()==null||taskgrid.getReceiverid().equals(""))){
                final String[] ss = taskgrid.getReceiverid().split(",");
                final String[] sn = taskgrid.getReceivername().split(",");
                for ( int i = 0; i <ss.length ; i++) {
                     final int j = i;
                    final Taskgrid t = new Taskgrid();
                    RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/getListByTaskidAndPuserid");
                    params.addBodyParameter("taskid",String.valueOf(taskgrid.getId()));
                    params.addBodyParameter("puserid",String.valueOf(ss[i]));
                    NetUtil.postData(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            if(!(result==null||result.equals("\"\""))){
                                //=====================================================
                                /*if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
                                    FuncUtil.updateReadTask(String.valueOf(taskgrid.getOrataskid()));
                                }*/

                                Gson gson = new Gson();
                                Processtask processtask = gson.fromJson(result,Processtask.class);
                                t.setId(taskgrid.getId());
                                t.setUnitid(taskgrid.getUnitid());
                                t.setReceiverid(processtask.getPuserid());
                                t.setReceivername(processtask.getPname());
                                if(processtask.getStatus().equals("0")){
                                    t.setStatus("已提交信息");
                                }else
                                if(processtask.getStatus().equals("1")){
                                    t.setStatus("民警已采纳");
                                }else
                                if(processtask.getStatus().equals("2")){
                                    t.setStatus("驳回需重传");
                                }else
                                if(processtask.getStatus().equals("3")){
                                    t.setStatus("未采纳");
                                }else
                                if(processtask.getStatus().equals("4")){
                                    t.setStatus("民警已执行");
                                }

                                list.add(t);
                                if(j==ss.length-1){
                                    adapter = new ProcesstaskAdapter(list,ProcesstaskActivity.this);
                                    processtaskList.setAdapter(adapter);
                                }
                            }else{
                               /* if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
                                    FuncUtil.updateReadTask(String.valueOf(taskgrid.getOrataskid()));
                                }*/
                                t.setId(taskgrid.getId());
                                t.setUnitid(taskgrid.getUnitid());
                                t.setReceiverid(ss[j]);
                                t.setReceivername(sn[j]);
                                if(taskgrid.getQianshou().equals("1")){
                                    t.setStatus("已签收");
                                }else{
                                    t.setStatus("未提交信息");
                                }

                                list.add(t);
                                if(j==ss.length-1){
                                    adapter = new ProcesstaskAdapter(list,ProcesstaskActivity.this);
                                    processtaskList.setAdapter(adapter);
                                }
                            }

                        }
                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {}
                        @Override
                        public void onCancelled(CancelledException cex) {}
                        @Override
                        public void onFinished() {}
                    });

                }

            }else{
                RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/getListByTaskidAndPuserid");
                params.addBodyParameter("taskid",String.valueOf(taskgrid.getId()));
                params.addBodyParameter("puserid",userid);
                NetUtil.postData(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if(!(result==null||result.equals("\"\""))){
                            /*if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
                                FuncUtil.updateReadTask(String.valueOf(taskgrid.getOrataskid()));
                            }*/
                            Gson gson = new Gson();
                            final ArrayList<Processtask> list1 = new ArrayList<Processtask>();
                            Processtask processtask = gson.fromJson(result,Processtask.class);
//                            processtask.setId(taskgrid.getId());
                            if(processtask.getStatus().equals("4")){
                                processtask.setStatus("民警已执行");
                            }
                            list1.add(processtask);
                            Processtask1Adapter adapter1 = new Processtask1Adapter(list1,ProcesstaskActivity.this);
                            processtaskList.setAdapter(adapter1);
                        }

                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {}
                    @Override
                    public void onCancelled(CancelledException cex) {}
                    @Override
                    public void onFinished() {}
                });
            }





            /*RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/getListByTaskidAndUnitid");
            params.addBodyParameter("taskid",String.valueOf(taskgrid.getId()));
            params.addBodyParameter("regionid",String.valueOf(taskgrid.getUnitid()));
            NetUtil.postData(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    if(result!=null){
                        Gson gson = new Gson();
                        Type listType = new TypeToken<ArrayList<Processtask>>() {
                        }.getType();
                        final ArrayList<Processtask> list = gson.fromJson(result,listType);
                        adapter = new ProcesstaskAdapter(list,ProcesstaskActivity.this);
                        processtaskList.setAdapter(adapter);
                        processtaskList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent();
                                intent.putExtra("processtask",list.get(i));
                                intent.setClass(ProcesstaskActivity.this,ProcessdetailActivity.class);
                                startActivity(intent);
                            }
                        });
                    }
                }
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {}
                @Override
                public void onCancelled(CancelledException cex) {}
                @Override
                public void onFinished() {}
            });*/
        }
    }
}
